package com.syq.web;

import com.syq.bean.Grade;
import com.syq.bean.Menu;
import com.syq.bean.Role;
import com.syq.bean.Users;
import com.syq.service.GradeService;
import com.syq.service.MenuService;
import com.syq.service.RoleService;
import com.syq.service.UsersService;
import com.syq.service.impl.GradeServiceImpl;
import com.syq.service.impl.MenuServiceImpl;
import com.syq.service.impl.RoleServiceImpl;
import com.syq.service.impl.UsersServiceImpl;
import com.syq.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = "/power/role/roles")
public class RoleServlet extends HttpServlet {
    private RoleService roleService = new RoleServiceImpl();
    private MenuService menuService = new MenuServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("select".equals(method)) {
            select(req,resp);
        }else if("selectmenus".equals(method)) {
            selectMenus(req,resp);
        } else if("insert".equals(method)) {
            addRole(req,resp);
        } else if("info".equals(method)) {
            getInfo(req,resp);
        } else if("update".equals(method)) {
            updateRole(req,resp);
        } else if("findbyid".equals(method)) {
            findById(req,resp);
        } else if("deleterole".equals(method)) {
            deleteRole(req,resp);
        } else if("startrole".equals(method)) {
            startRole(req,resp);
        }
    }
    //开启权限
    protected void startRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("roleId");
        int i = roleService.startRole(Integer.parseInt(roleId));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('启用成功');location.href='/power/role/roles?method=select'</script>");
        }else{
            writer.println("<script>alert('启用失败');location.href=/power/role/roles?method=select'</script>");
        }
    }
        protected void deleteRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("roleId");
        int i = roleService.deleteRole(Integer.parseInt(roleId));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('删除成功');location.href='/power/role/roles?method=select'</script>");
        }else{
            writer.println("<script>alert('删除失败');location.href=/power/role/roles?method=select'</script>");
        }


    }
    //主键查找
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("roleId");
        Role role = roleService.findById(Integer.parseInt(roleId));
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menulist",menuList);
        req.setAttribute("r",role);
        List<Menu> menus = role.getMenuList();

        List menu = new ArrayList();
        for(Menu m : menus) {
            menu.add(m.getMenuId());
            for(Menu m2 : m.getSecondMenuList()){
                menu.add(m2.getMenuId());
            }
        }
        req.setAttribute("menuIds",menu);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }
    //更新
    protected void updateRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("roleId");
        String roleName = req.getParameter("roleName");
        String state = req.getParameter("state");
        String[] menuids = req.getParameterValues("menuid");
        Role role = new Role();
        role.setRoleId(Integer.parseInt(roleId));
        role.setRoleName(roleName);
        role.setRoleState(Integer.parseInt(state));
        int i = roleService.updateRole(role,menuids);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('修改成功');location.href='/power/role/roles?method=select'</script>");
        }else{
            writer.println("<script>alert('修改失败');location.href='/power/role/roles?method=findbyid&roleId="+roleId+"'</script>");
        }
//        if(i>0) {
//            resp.sendRedirect("/power/role/roles?method=select");
//        }else {
//            resp.sendRedirect("/power/role/roles?method=findbyid&roleId="+roleId);
//        }

    }
    //详细信息
    protected void getInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("roleId");
//        System.out.println("wolaile"+roleId);
        Role role = roleService.findById(Integer.parseInt(roleId));
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menulist",menuList);
        req.setAttribute("r",role);
        List<Menu> menus = role.getMenuList();
        List secondM = new ArrayList();

        List menu = new ArrayList();
        for(Menu m : menus) {
           menu.add(m.getMenuId());
           for(Menu m2 : m.getSecondMenuList()){
               menu.add(m2.getMenuId());
           }
        }
        req.setAttribute("menuIds",menu);
        req.getRequestDispatcher("info.jsp").forward(req,resp);
    }
    //添加角色
    protected void addRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleName = req.getParameter("rolename");
        String state = req.getParameter("state");
        String[] menuids = req.getParameterValues("menuid");
        int i = roleService.addRole(roleName,state,menuids);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('添加成功');location.href='/power/role/roles?method=select'</script>");
        }else{
            writer.println("<script>alert('添加失败');location.href='/power/role/roles?method=selectmenus'</script>");
        }
//        if(i>0) {
//            resp.sendRedirect("/power/role/roles?method=select");
//        }else {
//            resp.sendRedirect("/power/role/roles?method=selectmenus");
//        }
    }
        //查询菜单列表
    protected void selectMenus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menulist",menuList);
        req.getRequestDispatcher("add.jsp").forward(req,resp);
    }
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        RoleService roleService = new RoleServiceImpl();
//        List<Role> roles = roleService.getList();
//        req.setAttribute("rlist",roles);
//        req.getRequestDispatcher("add.jsp").forward(req,resp);

        //接收参数
        String index = req.getParameter("index");
        //System.out.println("intdex:"+index);
        int pageIndex = (index==null||index.length()==0)?1:Integer.parseInt(index);
        //查询数据列表
        PageUtil pageUtil = new PageUtil();
        pageUtil.setPageIndex(pageIndex);
        List<Role> roleList = roleService.getRoleList(pageIndex, pageUtil.getPageSize());
        //System.out.println(usersList.get(0).getUserId());
        //总条数
        int total = roleService.total();
        pageUtil.setTotal(total);
        pageUtil.setDataList(roleList);
        //重定向
        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
}
